package com.fosun.spr.core.utils;

import org.apache.commons.beanutils.BeanUtils;

import com.fosun.spr.core.base.model.CoBaseSpModel;
import com.fosun.spr.core.base.model.CoBaseSrModel;
import com.fosun.spr.core.constants.DataConstants;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.message.ResultCode;
 

/***
 * 
 * @Title: ExtAsserts.java
 * @Description: 业务参数校验 #.必要业务参数
 * @author zhangcheng
 * @date 2017年3月7日 下午9:26:30
 * @version V1.0
 * 
 */
public class ExtAsserts {
	
	/**
	 * 记录存在，且isDeleted='0'
	 * @param obj
	 * @param id
	 */
	public static void dbRowExistsByIsDeleted(Object obj,Object id){
		try {
			String isDeleted = BeanUtils.getProperty(obj, "isDeleted");
			if(obj!=null && "0".equals(isDeleted)){
				//OK
			}else{
				throw new ServiceException(ResultCode.DBROW_NOTEXISTS_ERROR,id.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(ResultCode.DBROW_NOTEXISTS_ERROR,id.toString());
		}
	}

	public static void notNull(Object obj,String fieldName) {
		if (obj == null){
			throw new ServiceException(ResultCode.PARAMTER_ERROR,fieldName);
		}		
	}
	public static void notNull(Object obj) {
		if (obj == null){
			throw new ServiceException(ResultCode.PARAMTER_ERROR);
		}
	}

	public static void idNotNull(Object obj) {
		notNull(obj);
		if(!(obj instanceof CoBaseSpModel)){
			throw new ServiceException(ResultCode.SP_MODEL_TYPE_ERROR);
		}
		CoBaseSpModel spmodel = (CoBaseSpModel) obj;
		if (spmodel.realId() == null){
			throw new ServiceException(ResultCode.PARAMTER_ERROR);
		}
	}
	
	public static void isSucessTag(CoBaseSrModel srModel) {
		if(srModel==null) {
			throw new ServiceException(ResultCode.SR_MODEL_ERROR_NULL);
		}
		if(srModel.getErrcode()==null || !srModel.getErrcode().equals("0")) {
			throw new ServiceException(srModel.getErrcode(),srModel.getErrmsg());
		}
	}

	public static void updData(int record) {
		if (record <= DataConstants.NUM_0){
			throw new ServiceException(ResultCode.DB_UPDATE_ERROR);
		}
	}
	
	public static void equals(Object src,Object dst){
		if( (src==null && dst==null) || src.equals(dst)) {
			//OK
		}else{
			throw new ServiceException(ResultCode.ILLEGAL_REQUEST_ERROR);
		}
	}

}
